﻿// Copyright (c) 2008, Colin Burn
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 
// * Neither the name of WTCL nor the names of its contributors may be
// used to endorse or promote products derived from this software
// without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

using WTCL.Charts.Axis.Data;
using WTCL.Charts.Axis.Scale;
using WTCL.Charts.Axis.Ticks;
using WTCL.Data.Series.Group;
using WTCL.Data.Type;

namespace WTCL.Charts.StackedBarChart
{
    public class StackedBarChartCanvas<TType, TSpanType> : GroupAxisCanvas<TType, TSpanType,
            GroupSeries<TType, TSpanType>,
            GroupData<TType, TSpanType>,
            BaseStackedBarChart<TType, TSpanType>,
            StackedBarChartCanvas<TType, TSpanType>>
    {
        private List<GroupData<TType, TSpanType>> groupData = new List<GroupData<TType, TSpanType>>();

        internal override AxisScale<BaseStackedBarChart<TType, TSpanType>, ValueAxisData<TType, TSpanType>, YAxisTick> CreateYScale(BaseStackedBarChart<TType, TSpanType> chart)
        {
            return chart.CreateYScale();
        }

        protected override void OnRender(DrawingContext dc)
        {
            foreach (GroupData<TType, TSpanType> data in this.groupData)
            {
                data.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(OnDataPropertyChanged);
            }

            this.groupData.Clear();

            if (this.ActualWidth > 0 && this.ActualHeight > 0)
            {
                base.DrawBorder(dc, new Pen(new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0x80)), 1));
                base.DrawGrid(dc, new Pen(new SolidColorBrush(Color.FromArgb(0xFF, 0x80, 0x80, 0x80)), 1));

                int yValueCount = 0;
                for (int j = 0; j < this.Chart.Series.Count; ++j)
                {
                    yValueCount = Math.Max(this.Chart.Series[j].Samples.Count, yValueCount);
                }

                double[] barBase = new double[yValueCount];

                double barSpacing = this.ActualWidth / yValueCount;

                List<ValueAxisData<TType, TSpanType>> axisData = this.YAxisScale.AxisData;
                for (int j = 0; j < axisData.Count; ++j)
                {
                    if (axisData[j] != null)
                    {
                        IDataType<TType, TSpanType> minValue = axisData[j].ActualMinValue;
                        IDataType<TType, TSpanType> maxValue = axisData[j].ActualMaxValue;

                        for (int i = 0; i < this.Chart.Series[j].Samples.Count; ++i)
                        {
                            GroupData<TType, TSpanType> data = this.Chart.Series[j].Samples[i];
                            data.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnDataPropertyChanged);

                            this.groupData.Add(data);

                            double dx = (barSpacing * i) + 1;

                            double barHeight = 0;

                            if (data.Value != null)
                            {
                                barHeight = this.ActualHeight * data.Value.Abs().GetRatio(null, maxValue);
                            }

                            double dy = this.ActualHeight - (barHeight + barBase[i]);

                            barBase[i] = barHeight + barBase[i];

                            RectangleGeometry rectGeometry = new RectangleGeometry();
                            rectGeometry.RadiusX = 2;
                            rectGeometry.RadiusY = 2;
                            rectGeometry.Rect = new Rect(dx, dy, barSpacing - 2, barHeight);

                            dc.DrawGeometry(this.Chart.Series[j].Brush, this.Chart.Series[j].Pen, rectGeometry);
                        }
                    }
                }
            }
        }

        private void OnDataPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Value")
            {
                base.InvalidateData();
            }
        }

        internal override void HintSeriesChanged()
        {
            InvalidateVisual();

            base.HintSeriesChanged();
        }

        internal override void HintSeriesChanged(int series, int index)
        {
            InvalidateVisual();

            base.HintSeriesChanged(series, index);
        }
    }
}
